/**
 * @author Isabella Nake (isabella.nake@usi.ch)
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.gui.tool;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import ch.usi.inf.pf2.circuit.Component;
import ch.usi.inf.pf2.circuit.Gate;
import ch.usi.inf.pf2.gui.Model;

public class SetDelayTool extends Tool{

	private Gate currentGate;
	
	public SetDelayTool(final Model model){
		super(model, "Select this tool and click on a gate to change the delay.", "../icons/delay1.png", "../icons/delay1Bg.png");
        currentGate = null;
	}
	
	public void mouseClicked(final MouseEvent ev) {
		model.clearSelection();
		currentGate = null;
        final int x = ev.getX();
        final int y = ev.getY();
        final Component shape = getCircuit().findShapeAt(x, y);
        if (shape instanceof Gate){
        	currentGate = ((Gate) shape);
        	//String strDelay = JOptionPane.showInputDialog("Enter new delay", currentGate.getDelay());
        	ImageIcon icon = new ImageIcon(getClass().getResource("../icons/SetDelayDialog.png"));
        	String strDelay = (String)JOptionPane.showInputDialog( null, "Enter new delay", "", JOptionPane.QUESTION_MESSAGE, icon, null, currentGate.getDelay());
        	
        	boolean validInput = true;
        	if(strDelay != null) {
        		String numbers = "0123456789";
        		for(int i = 0; i<strDelay.length(); i++) {
        			if(numbers.indexOf(strDelay.charAt(i)) == -1) {
        				validInput = false;
        				break;
        			}
        		}
	        	
	        	System.out.println(strDelay);
	        	
	        	if(validInput){
	        		int delay = Integer.parseInt(strDelay);
	            	currentGate.setDelay(delay);	
	            	model.shapeChanged();
	        	} else {
        		}
        	}
        }
        else{
        }
	}
	

}
